package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.GeologicalDisasters;
import com.css.fxfzaqbz.modules.data.queryParams.GeologicalDisastersQueryParams;
import com.css.fxfzaqbz.modules.data.repository.GeologicalDisastersRepository;
import com.css.fxfzaqbz.modules.data.service.GeologicalDisastersService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@Service
public class GeologicalDisastersServiceImpl implements GeologicalDisastersService {
    @Resource
    GeologicalDisastersRepository geologicalDisastersRepository;

//    @Override
//    public GasstationDataVO updateGasstationData(GasstationDataVO gasstationDataVO) {
//
//        GasstationDataVO elevationVO1 = gasstationDataRepository.updateGasstationData(gasstationDataVO);
//
//        return elevationVO1;
//    }

    @Override
    @Transactional
    public GeologicalDisasters saveGeologicalDisasters(GeologicalDisasters geologicalDisasters) {
        geologicalDisasters.setGdId(UUIDGenerator.getUUID());
        geologicalDisasters.setGdStatus(YNEnum.Y.toString());
        geologicalDisasters.setGdCreatetime(PlatformDateUtils.getCurrentTimestamp());
        return geologicalDisastersRepository.saveGeologicalDisasters(geologicalDisasters);
    }

    @Override
    public JSONObject getAllGeologicalDisasters(GeologicalDisastersQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(geologicalDisastersRepository.getAllGeologicalDisastersParams(queryParams));
    }

    @Override
    public GeologicalDisasters getGeologicalDisastersById(String id) {
        GeologicalDisasters geologicalDisasters = geologicalDisastersRepository.getGeologicalDisastersById(id);
        return geologicalDisasters;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGeologicalDisasters(List<String> ids) {
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        geologicalDisastersRepository.deleteGeologicalDisastersById(ids,currentTimestamp);
    }

    @Override
    public List<GeologicalDisasters> getAll() {
        return geologicalDisastersRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<GeologicalDisasters> geologicalDisasters = FileUtil2.importExcel(file, GeologicalDisasters.class);

        for(int i = 0 ; i<geologicalDisasters.size();i++){
            geologicalDisasters.get(i).setGdId(UUIDGenerator.getUUID());
            geologicalDisasters.get(i).setGdCreatetime(PlatformDateUtils.getCurrentTimestamp());
            geologicalDisasters.get(i).setGdStatus(YNEnum.Y.toString());

        }
        geologicalDisastersRepository.importExcel(geologicalDisasters);
    }
}

